<?php

namespace App\Models\RBAC;

use Illuminate\Support\Facades\Config;
use Spatie\Permission\Models\Role as BaseRole;

/**
 * Class Role.
 *
 * @package namespace App\Models;
 */
class Role extends BaseRole
{
    const TABLE_NAME = '';

    protected $fillable = [
        'guard_name',
        'name',
        'display_name',
        'description',
    ];

    const KEY_ROLE_MENUS = 'role_menus';

    /****************** Relationship *********************/

    /****************** Attribute *********************/

    /****************** Scope *********************/

    /****************** Function *********************/

    public function perms($root_id = 0)
    {
        $tableNames = Config::get('permission.table_names');
//        $rs = $this->belongsToMany($tableNames['permissions'], $tableNames['role_has_permissions'])->orderBy('position',
//            "desc")->orderBy('id', "asc");

        $rs = $this->belongsToMany('App\Models\RBAC\Permission', $tableNames['role_has_permissions'])->orderBy('position',
            "desc")->orderBy('id', "asc");


        if (!empty($root_id)) {
            // todo Zhangxy 其实这儿还可以稍微扩展一下 适用于所有字段
            $rs = $rs->wherePivot('root_permission_id', $root_id);
        }
        return $rs;
    }

}
